// 个人计划控制器
define('salesPersonPlanController', ['angular','jquery'], function (angular,$) {
    var module = angular.module('controllers');
    module.controller('salesPersonPlanController', ['$scope','$rootScope','$filter','$route','$routeParams','userServers','salesServers',function ($scope,$rootScope,$filter,$route,$routeParams,userServers,salesServers) {
        $scope.setData={};
        $scope.table_list=[];
        $scope.currentData=[];
        $scope.planList={
            branch_company_id:"",
            user_id:"",
            month:""           
            // type:2
        };
        $scope.userList=[];
        $scope.searchSalesForm=function(){
            $scope.planList = $filter('simplifyJsonFilter')($scope.planList);
            salesServers.searchSales($scope.planList).then(function(data) {
                $scope.table_list=data;
                userServers.userList().then(function (data) {
                    $scope.userList=data;
                    for(var i=0;i<$scope.table_list.length;i++){
                        for(var j=0;j<$scope.userList.length;j++){
                            if($scope.table_list[i].user_id==$scope.userList[j].id){
                                $scope.table_list[i].user_name=angular.copy($scope.userList[j].name);
                            }
                        }
                    }                    
                });
            });
        };
        $scope.searchSalesForm();
        $scope.$watch("currentData",function(n,o){
            $scope.sum();
        });
        $scope.sum=function(){
            $scope.sumData={
                guarantee_planning:0,
                task_planning:0,
                target_planning:0,
                reality_money:0
            };
            for (var i = 0; i < $scope.currentData.length; i++) {
                  $scope.sumData.guarantee_planning+=$scope.currentData[i].guarantee_planning;
                  $scope.sumData.task_planning+=$scope.currentData[i].task_planning;
                  $scope.sumData.target_planning+=$scope.currentData[i].target_planning;
                  $scope.sumData.reality_money+=$scope.currentData[i].reality_money;
            }
        };
        // 显示模态框，给框里设置当前选中的值
        $scope.setplan=function(v){
            $scope.setSalesData=angular.copy(v);
        };
        //修改
        $scope.setForm=function(){
            $scope.setSalesData.branch_company="";
            delete $scope.setSalesData.branch_company;
            $scope.setSalesData = $filter('simplifyJsonFilter')($scope.setSalesData);
            salesServers.setplan($scope.setSalesData).then(function(data){
                if(data.data===true){
                    $route.reload();
                    $("#myModal").modal('hide');
                }else{
                    alert('失败');
                }
            });
        };
        //删除
        $scope.delplan=function(v){
            if(confirm('确定删除?')===false)
                return false;
            salesServers.delplan(v).then(function(data) {
                if(data.data===true){
                    $route.reload();
                }else{
                    alert('失败');
                }
            });                 
        }; 
    }]);
});